﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace UserKTXD
{
    public partial class Chi_Tiet_Chuyen_Muc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["idSubChuyenMuc"] == null)
                {
                    HienThiChuyenMuc(Convert.ToInt32(Request.Params["Id"].ToString()));
                }
                else
                {
                    HienThiChuyenMucCapHai(Convert.ToInt32(Request.Params["Id"].ToString()));
                }
            }
        }
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        private void HienThiChuyenMuc(int Id)
        {
            //ChuyenMuc ent_ChuyenMuc = DataRepository.ChuyenMucProvider.GetById(Id);
            //lbtieude.Text = ent_ChuyenMuc.TieuDe.ToString();
            //lbtimer.Text = ent_ChuyenMuc.NgayCapNhat.ToShortDateString();
            //lbLuotxem.Text = ent_ChuyenMuc.LuotXem.ToString();
            //lbNdung.Text = Server.HtmlDecode(ent_ChuyenMuc.NoiDung.ToString());
            //LoaiChuyenMuc ent_LChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(Convert.ToInt32(ent_ChuyenMuc.IdLoaiChuyenMuc.ToString()));
            //lbtag.Text = ent_LChuyenMuc.TenLoaiChuyenMuc.ToString();
            TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.Find("Id=" + Id);
            if (TList_ChuyenMuc.Count > 0)
            {
                litDeception.Text = String.Format("<meta name=\"description\" content=\"{0}\" />", TList_ChuyenMuc[0].TomTat);
                litTieuDe.Text = TList_ChuyenMuc[0].TieuDe;
                int _view = 0;
                _view += TList_ChuyenMuc[0].LuotXem;
                _view += 1;
                string query = string.Empty;
                
                conn.Open();
                query = "UPDATE ChuyenMuc SET LuotXem = '" + _view + "' where Id = '" + TList_ChuyenMuc[0].Id + "'";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
                rptBaiViet.DataSource = TList_ChuyenMuc;
                rptBaiViet.DataBind();
            }
        }

        private void HienThiChuyenMucCapHai(int Id)
        {
            //ChuyenMucCapHai ent_ChuyenMucCapHai = DataRepository.ChuyenMucCapHaiProvider.GetById(Id);
            //lbtieude.Text = ent_ChuyenMucCapHai.TieuDe.ToString();
            //lbtimer.Text = ent_ChuyenMucCapHai.NgayCapNhat.ToShortDateString();
            //lbLuotxem.Text = ent_ChuyenMucCapHai.LuotXem.ToString();
            //lbNdung.Text = Server.HtmlDecode(ent_ChuyenMucCapHai.NoiDung.ToString());
            //SubChuyenMuc ent_SChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(Convert.ToInt32(ent_ChuyenMucCapHai.IdSubChuyenMuc.ToString()));
            //lbtag.Text = ent_SChuyenMuc.TenSubChuyenMuc.ToString();
            TList<ChuyenMucCapHai> TList_ChuyenMucCapHai = DataRepository.ChuyenMucCapHaiProvider.Find("Id=" + Id);
            if (TList_ChuyenMucCapHai.Count>0)
            {
                litDeception.Text = String.Format("<meta name=\"description\" content=\"{0}\" />", TList_ChuyenMucCapHai[0].TomTat);
                litTieuDe.Text = TList_ChuyenMucCapHai[0].TieuDe;
                //Tăng Lượt Xem
                int _view = 0;
                _view += TList_ChuyenMucCapHai[0].LuotXem;
                _view += 1;
                string query = string.Empty;

                conn.Open();
                query = "UPDATE ChuyenMucCapHai SET LuotXem = '" + _view + "' where Id = '" + TList_ChuyenMucCapHai[0].Id + "'";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
                rptBaiViet.DataSource = TList_ChuyenMucCapHai;
                rptBaiViet.DataBind();
            }
        }

        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }

        public string GetTenLoaiChuyenMuc(Object ob)
        {
            return DataRepository.LoaiChuyenMucProvider.GetById(Convert.ToInt32(ob.ToString())).TenLoaiChuyenMuc;
        }

        public string GetTenSubChuyenMuc(Object ob)
        {
            return DataRepository.SubChuyenMucProvider.GetById(Convert.ToInt32(ob.ToString())).TenSubChuyenMuc;
        }

        public string GetTag(Object ob)
        {

            /*Cắt chuỗi*/
            string[] arrayStr;
            if (Request.Params["idSubChuyenMuc"] == null)
                arrayStr =DataRepository.ChuyenMucProvider.GetById(Convert.ToInt32(ob.ToString())).TagCloud.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            else
                arrayStr = DataRepository.ChuyenMucCapHaiProvider.GetById(Convert.ToInt32(ob.ToString())).TagCloud.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string chuoi = "";
            //Lấy tên đệm
            for (int i = 0; i < arrayStr.Length; i++)
            {
                if (i == arrayStr.Length - 1)
                    chuoi += "<a href='../../../TimKiem/" + GetTieuDe(arrayStr[i]) + ".aspx'>" + arrayStr[i] + "<a/>";
                else
                    chuoi += "<a href='../../../TimKiem/" + GetTieuDe(arrayStr[i]) + ".aspx'>" + arrayStr[i] + "<a/>,";
            }
            return chuoi;
        }

        public string GetTenTacGia(object Id)
        {
            int s = Convert.ToInt32(Id.ToString());
            ThanhVien ent_ThanhVien = DataRepository.ThanhVienProvider.GetById(s);
            return ent_ThanhVien.HoTen;
        }
    }
}